package com.dh.bm.dao.impl;

import com.dh.bm.common.JDBCUtil;
import com.dh.bm.dao.BookDao;
import com.dh.bm.entity.Book;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {
    @Override
    public List<Book> selectAll() {
        // 用jdbc取数据库查询数据
        List<Book> books = new ArrayList<>();
        String sql = "select * from book where is_delete = 0";
        PreparedStatement ps = JDBCUtil.getStatement(sql);
        ResultSet resultSet = null;
        try {
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer bid = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String author = resultSet.getString(3);
                Integer stock = resultSet.getInt(4);
                Integer cid = resultSet.getInt(5);
                String created_time = resultSet.getString(6);
                String updated_time = resultSet.getString(7);
                //  把数据库读出来的每一行数据都转换成一本书的对象
                Book book = new Book();
                book.setBid(bid);
                book.setName(name);
                book.setAuthor(author);
                book.setStock(stock);
                book.setCid(cid);
                book.setCreatedTime(created_time);
                book.setUpdatedTime(updated_time);
                books.add(book);// 把书对象加到集合中
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(resultSet);
        }
        return books;
    }

    @Override
    public Book selectById(int id) {
        String sql = "select * from book where bid =?";
        PreparedStatement ps = JDBCUtil.getStatement(sql);
        ResultSet resultSet = null;
        try {
            ps.setInt(1, id);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Integer bid = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String author = resultSet.getString(3);
                Integer stock = resultSet.getInt(4);
                Integer cid = resultSet.getInt(5);
                String created_time = resultSet.getString(6);
                String updated_time = resultSet.getString(7);
                //  把数据库读出来的每一行数据都转换成一本书的对象
                Book book = new Book();
                book.setBid(bid);
                book.setName(name);
                book.setAuthor(author);
                book.setStock(stock);
                book.setCid(cid);
                book.setCreatedTime(created_time);
                book.setUpdatedTime(updated_time);
                return book;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(resultSet);
        }
        return null;
    }

    @Override
    public boolean updateById(int id, Book book) {
        String sql = "update book set name=?,author=?,stock=?,updated_time=? where bid = ?";
        PreparedStatement ps = JDBCUtil.getStatement(sql);
        try {
            ps.setString(1, book.getName());
            ps.setString(2, book.getAuthor());
            ps.setInt(3, book.getStock());
            ps.setString(4, book.getUpdatedTime());
            ps.setInt(5, book.getBid());
            return ps.executeUpdate() > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean deleteById(int id) {
        return false;
    }

    @Override
    public boolean insert(Book book) {
        String sql = "insert into book (name,author,stock,c_id,created_time,updated_time) values(?,?,?,?,?,?)";
        PreparedStatement ps = JDBCUtil.getStatement(sql);
        try {
            ps.setString(1, book.getName());
            ps.setString(2, book.getAuthor());
            ps.setInt(3, book.getStock());
            ps.setInt(4, book.getCid());
            ps.setString(5, book.getCreatedTime());
            ps.setString(6, book.getUpdatedTime());
            int i = ps.executeUpdate();
            return i > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close();
        }

        return false;
    }
}
